package com.jiinfo.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jiinfo.util.UUIDUtils;

public class Resource extends Model<Resource>{

	private static final long serialVersionUID = -8869654648253040661L;
	public static final Resource dao = new Resource();

	
	public Page<Record> pagequery(Integer pageNumber,Integer pageSize,Record record){
		StringBuffer sqlExceptSelect=new StringBuffer("from t_resource t where t.removed='0' ");
		List<Object> list=new ArrayList<Object>();
		
		
		sqlExceptSelect.append(" order by t.updateTime desc");
		
		if(pageNumber==null){
			pageNumber=1;
		}
		if(pageSize==null){
			pageSize=10;
		}
		return Db.paginate(pageNumber,pageSize,"select t.*",sqlExceptSelect.toString(),list.toArray());
	}
	
	public Resource add(Resource vo,Record user){
		Resource resource=new Resource();
		resource.set("id", UUIDUtils.getUUID());
		resource.set("createTime", new Date());
		resource.set("creator", user.getStr("id"));
		resource.set("updateTime", new Date());
		resource.set("updator", user.getStr("id"));
		resource.set("removed", "0");
		
		resource.set("name", vo.getStr("name"));
		resource.set("code", vo.getStr("code"));
		resource.set("url", vo.getStr("url"));
		resource.set("icon",vo.getStr("icon"));
		resource.set("visibility",vo.getStr("visibility"));
		resource.set("px",vo.getInt("px"));
		resource.set("parentId",vo.getStr("parentId"));
		
		resource.save();
		return resource;
	}
	
	public Resource edit(Resource vo,Record user){
		Resource resource=Resource.dao.findById(vo.getStr("id"));
		resource.set("updateTime", new Date());
		resource.set("updator", user.getStr("id"));
		
		resource.set("name", vo.getStr("name"));
		resource.set("code", vo.getStr("code"));
		resource.set("url", vo.getStr("url"));
		resource.set("icon",vo.getStr("icon"));
		resource.set("visibility",vo.getStr("visibility"));
		resource.set("px",vo.getInt("px"));
		resource.set("parentId",vo.getStr("parentId"));
		
		resource.update();
		return resource;
	}
	
	public Resource remove(String id){
		Resource resource=Resource.dao.findById(id);
		resource.set("removed", "1");
		resource.update();
		return resource;
	}
	
	public List<Record> getResourcesByRoleId(String roleId){
		String sql="select r.* from t_resource r "
				+ "inner join t_role_resource rr on r.id=rr.resourceId and rr.roleId=? "
				+ "where r.removed='0' ";
		return Db.find(sql,roleId);
	}
	
	public List<Record> getAllResources(){
		String sql="select * from t_resource r where r.removed='0' ";
		return Db.find(sql);
	}
	
	public List<Record> getTopResources(){
		String sql="select r.* from t_resource r "
				+ " where r.removed='0' "
				+ " and (r.parentId is null or r.parentId='' )  "
				+ " order by r.px  ";
		return Db.find(sql);
	}
	
	public List<Record> getAllResourcesByParentId(String parentId){
		String sql="select r.* from t_resource r "
				+ " where r.removed='0' "
				+ " and r.parentId=? "
				+ " order by r.px  ";
		return Db.find(sql,parentId);
	}
	
	public Record getFullTreeResource(String childrenName,int parentLevel,Record record){
		List<Record> subList=getAllResourcesByParentId(record.getStr("id"));
		List<Record> list=new ArrayList<Record>();
		int subLevel=parentLevel+1;
		if(subList.size()>0){
			for (Record record2 : subList) {
				record.set("treeLevel", subLevel);
				list.add(getFullTreeResource(childrenName,subLevel,record2));
			}
		}
		record.set(childrenName, list);
		record.set("treeLevel", parentLevel);
		return record;
	}
	
	public List<Record> getTreeResources(String childrenName){
		List<Record> list=getTopResources();
		int parentLevel=1;
		for (Record record : list) {
			getFullTreeResource(childrenName,parentLevel,record);
		}
		return list;
	}
	
	public List<Record> getTopResourcesByUserId(String userId){
		String sql="select r.* from t_resource r "
				+ " inner join t_role_resource rr on r.id=rr.resourceId "
				+ " inner join t_user_role ur on ur.roleId=rr.roleId "
				+ " inner join t_user u on u.id=ur.userId "
				+ " where r.removed='0' "
				+ " and rr.removed='0'"
				+ " and ur.removed='0'"
				+ " and u.removed='0' "
				+ " and u.id=?"
				+ " and (r.parentId is null or r.parentId='' )  "
				+ " order by r.px ";
		return Db.find(sql,userId);
	}
	
	public List<Record> getResourcesByUserIdAndParentId(String userId,String parentId){
		String sql="select r.* from t_resource r "
				+ " inner join t_role_resource rr on r.id=rr.resourceId "
				+ " inner join t_user_role ur on ur.roleId=rr.roleId "
				+ " inner join t_user u on u.id=ur.userId "
				+ " where r.removed='0' "
				+ " and rr.removed='0'"
				+ " and ur.removed='0'"
				+ " and u.removed='0' "
				+ " and u.id=?"
				+ " and r.parentId=? "
				+ " order by r.px  ";
		return Db.find(sql,userId,parentId);
	}
	
	public Record getFullTreeResourceByUserId(String userId,String childrenName,Record record,boolean visibility){
		List<Record> subList=getResourcesByUserIdAndParentId(userId,record.getStr("id"));
		List<Record> list=new ArrayList<Record>();
		if(subList.size()>0){
			for (Record record2 : subList) {
				if(visibility){
					if("0".equals(record2.getStr("visibility"))){
						list.add(getFullTreeResourceByUserId(userId,childrenName,record2,visibility));
					}	
				}else{
					list.add(getFullTreeResourceByUserId(userId,childrenName,record2,visibility));
				}
				
			}
		}
		record.set(childrenName, list);
		return record;
	}
	
	public List<Record> getTreeResourcesByUserId(String userId,String childrenName,boolean visibility){
		List<Record> list=getTopResourcesByUserId(userId);
		List<Record> records=new ArrayList<>();
		for (Record record : list) {
			if(visibility){
				if("0".equals(record.getStr("visibility"))){
					getFullTreeResourceByUserId(userId,childrenName,record,visibility);
					records.add(record);
				}
			}else{
				getFullTreeResourceByUserId(userId,childrenName,record,visibility);
				records.add(record);
			}
			
		}
		return records;
	}
	
	public void deleteNodeAndSubNode(String id){
		Resource.dao.deleteById(id);
		String selectSql="select * from t_resource r where r.parentId=?";
		List<Record> records=Db.find(selectSql,id);
		for (Record record : records) {
			Resource.dao.deleteById(record.getStr("id"));
			List<Record> subRecords=Db.find(selectSql,record.getStr("id"));
			if(subRecords.size()>0){
				deleteNodeAndSubNode(id);
			}
		}
	}
	
	
}
